package com.geovis.emergency.spd.biz.pc.wrapper;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.emergency.spd.biz.pc.service.IPcDisasterEventService;
import com.geovis.emergency.spd.cache.DictCache;
import com.geovis.emergency.spd.entity.pc.entity.PcDisaster;
import com.geovis.emergency.spd.entity.pc.entity.PcDisasterEvent;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcDisasterConstant;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcDisasterVO;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;

/**
 * <p>
 * 灾害事件包装类
 * </p>
 *
 * @author Lonsho
 * @version 1.0
 * @date 2022/6/10
 */
@Log4j2
@AllArgsConstructor
public class PcDisasterWrapper extends BaseEntityWrapper<PcDisaster, PcDisasterVO>{

    private final IPcDisasterEventService pcDisasterEventService;

    public static PcDisasterWrapper build(IPcDisasterEventService pcDisasterEventService) {
        return new PcDisasterWrapper(pcDisasterEventService);
    }
    @Override
    public PcDisasterVO entityVO(PcDisaster entity) {
        PcDisasterVO pcDisasterVO = BeanUtil.toBean(entity, PcDisasterVO.class);
        if (StrUtil.isNotBlank(entity.getType())){
            String dictValue = DictCache.getDictValue(PcDisasterConstant.DISASTER_TYPE, entity.getType());
            pcDisasterVO.setTypeName(dictValue);
        }
        pcDisasterVO.setEventCount(
                pcDisasterEventService.count(Wrappers.<PcDisasterEvent>lambdaQuery()
                        .eq(PcDisasterEvent::getPcDisasterId,entity.getId()))
        );
        return pcDisasterVO;
    }
}
